package com.joysuch.wwyt.attendance.task;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.attendance.service.AttendanceUserService;
import com.joysuch.wwyt.bp.entity.BpEnterpriseInfo;
import com.joysuch.wwyt.bp.repository.BpEnterpriseInfoDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author ZhaoQiuWei
 * @Date 2022/3/3 14:44
 */
@Component("AttenDanceUserTask")
@Slf4j
public class AttenDanceUserTask {

    @Autowired
    private BpEnterpriseInfoDao bpEnterpriseInfoDao;

    @Autowired
    private AttendanceUserService userService;

    /**
     * 定时生成下个月人员固定排班
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    public void checkExpired() {
        List<BpEnterpriseInfo> all = bpEnterpriseInfoDao.findAll();
        if (!CollectionUtils.isEmpty(all)) {
            LocalDateTime dateTime = LocalDateTime.now();
            for (BpEnterpriseInfo info : all) {
                if (StringUtils.isBlank(info.getOrgCode()) || info.getTenentId() == null) {
                    continue;
                }
                String orgCode = info.getOrgCode().substring(0, 7);
                Long tenentId = info.getTenentId();
                if (StringUtils.isBlank(orgCode)||tenentId==null){
                    continue;
                }
                try {
                    userService.createUserTask(orgCode, tenentId, dateTime.getYear(), dateTime.getMonthValue());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    e.printStackTrace();
                    continue;
                }
            }
        }

    }

}
